<?php
/**
 * Created by PhpStorm.
 * User: wdt
 * Date: 2016/3/31
 * Time: 10:13
 */

namespace front\modules\web\controllers;


use front\controllers\WebBaseController;
use goods\models\ShopCategory;
use goods\services\ShopCategoryProductService;
use yii\helpers\ArrayHelper;

class CategoryController extends WebBaseController {

    /**
     * 商品分类页面
     * 包括类目，和品牌分类
     * @return string
     */
     public function actionCategoryList()
     {
         $shop_id = \Yii::$app->request->get('shop_id');
         if (empty($shop_id)) {
             WebBaseController::getAjaxData(1,'参数传递不正确');
         }

         $list = [];
         //获取商品类目
         $oneCategory = ShopCategoryProductService::getShopCategory($shop_id);
         foreach ($oneCategory as $k=>$v) {
             $list[$k]['cid'] = $oneCategory[$k]['shop_cid'];
             $list[$k]['name'] = ShopCategory::getCategoryInfo($v['shop_cid']);
             $thumb = ShopCategoryProductService::getCategoryThumb($v['shop_cid']);
             $list[$k]['img'] = $thumb['thumb'] .'@1e_1c_0o_0l_150h_150w_100q.jpg';
         }
//         if (empty($fid)) {
//             $twoCategory = ShopCategoryProductService::getTwoCategory($shop_id,$oneCategory[0]['fid']);
//         } else {
//             $twoCategory = ShopCategoryProductService::getTwoCategory($shop_id,$fid);
//         }

         //采用如下方式，可以去除重新重复的类目值，distinct无法实现查询三个字段，对其中的两个字段去重
//         $list = [];
//         foreach ($twoCategory as $item) {
//             $list[$item['cid']]['name'] = $item['c_name'];
//             $list[$item['cid']]['img'] = $item['thumb'];
//         }
         //默认加载第一个类目的下级类目
         return $this->render('productCategory',[
             'list' => $list,
             'shop_id' => $shop_id,
         ]);
     }

    /**
     * 根据店铺ID，类目ID加载对应的商品列表
     * @return string
     */
    public function actionProductList()
    {
        $data = \Yii::$app->request->get();
        if (empty($data['shop_id'])) {
            WebBaseController::getAjaxData(2,'参数有误');
        }

        $products = [];
        // 加载类目商品列表
        if (!empty($data['cid'])) {
            $products = ShopCategoryProductService::searchList(['shop_cid' => $data['cid'],'shop_id' => $data['shop_id']]);
            $products = ArrayHelper::toArray($products);
        }

        //加载品牌商品列表
//        else if (!empty($data['brand_vid'])) {
//            $products = ShopCategoryProductService::searchList(['brand_vid' => $data['brand_vid'],'shop_id' => $data['shop_id']]);
//            $products = ArrayHelper::toArray($products);
//        }
        return $this->render('productList',[
            'list' => $products,
        ]);

    }

    /**
     * 接收类目前台页面ajax参数请求，
     * 确定类目时候，选择品类和品牌返回对应格式的请求值
     */
    public function actionAjax()
    {
        $data = \Yii::$app->request->get();
        $list = [];

        // 根据shop_id，cid加载品类
        if ($data['type'] == 1) {
           $category = ShopCategoryProductService::getTwoCategory($data['shop_id'],$data['fid']);
            foreach ($category as $item) {
                $list[$item['cid']]['name'] = $item['c_name'];
                $list[$item['cid']]['img'] = $item['thumb'];
            }
        }

        // 根据shop_id,cid加载品牌
        if ($data['type'] == 2) {
            $brands = ShopCategoryProductService::getBrand($data['shop_id'],$data['fid']);

            // 获取名称的首字母
            function getFirstCharter($str){
                     if(empty($str)){return '';}
                      $fchar=ord($str{0});
                      if($fchar>=ord('A')&&$fchar<=ord('z')) return strtoupper($str{0});
                      $s1=iconv('UTF-8','gb2312',$str);
                      $s2=iconv('gb2312','UTF-8',$s1);
                      $s=$s2==$str?$s1:$str;
                      $asc=ord($s{0})*256+ord($s{1})-65536;
                     if($asc>=-20319&&$asc<=-20284) return 'A';
                     if($asc>=-20283&&$asc<=-19776) return 'B';
                     if($asc>=-19775&&$asc<=-19219) return 'C';
                     if($asc>=-19218&&$asc<=-18711) return 'D';
                     if($asc>=-18710&&$asc<=-18527) return 'E';
                     if($asc>=-18526&&$asc<=-18240) return 'F';
                     if($asc>=-18239&&$asc<=-17923) return 'G';
                     if($asc>=-17922&&$asc<=-17418) return 'H';
                     if($asc>=-17417&&$asc<=-16475) return 'J';
                     if($asc>=-16474&&$asc<=-16213) return 'K';
                     if($asc>=-16212&&$asc<=-15641) return 'L';
                     if($asc>=-15640&&$asc<=-15166) return 'M';
                     if($asc>=-15165&&$asc<=-14923) return 'N';
                     if($asc>=-14922&&$asc<=-14915) return 'O';
                     if($asc>=-14914&&$asc<=-14631) return 'P';
                     if($asc>=-14630&&$asc<=-14150) return 'Q';
                     if($asc>=-14149&&$asc<=-14091) return 'R';
                     if($asc>=-14090&&$asc<=-13319) return 'S';
                     if($asc>=-13318&&$asc<=-12839) return 'T';
                     if($asc>=-12838&&$asc<=-12557) return 'W';
                     if($asc>=-12556&&$asc<=-11848) return 'X';
                     if($asc>=-11847&&$asc<=-11056) return 'Y';
                     if($asc>=-11055&&$asc<=-10247) return 'Z';
                     return null;
            }

            // 根据首字母划分
            foreach($brands as $k=>$v) {
                switch(getFirstCharter($v['b_name'])) {
                    case 'A' :
                        $list['A'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'B' :
                        $list['B'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'C' :
                        $list['C'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'D' :
                        $list['D'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'E' :
                        $list['E'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'F' :
                        $list['F'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'G' :
                        $list['G'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'H' :
                        $list['H'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'I' :
                        $list['I'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'J' :
                        $list['J'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'K' :
                        $list['K'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'L' :
                        $list['L'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'M' :
                        $list['M'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'N' :
                        $list['N'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'O' :
                        $list['O'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'P' :
                        $list['P'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'Q' :
                        $list['Q'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'R' :
                        $list['R'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'S' :
                        $list['S'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'T' :
                        $list['T'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'U' :
                        $list['U'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'V' :
                        $list['V'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'W' :
                        $list['W'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'X' :
                        $list['X'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'Y' :
                        $list['Y'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'Z' :
                        $list['Z'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                }
            }
        }

        // 重新加载类目
        $oneCategory = ShopCategoryProductService::getOneCategory($data['shop_id']);
        $cate = [];
        foreach ($oneCategory as $item) {
            $cate[$item['fid']] = ['name' => $item['f_name']];
        }
        die(trim($_GET['funcName']).'('.json_encode(array('type'=> $data['type'],'cate' => $cate, 'list' => $list)).')');
    }

    /**
     * 接收类目前台页面ajax参数请求，
     * 不确定类目时候，选择类目和品牌返回对应格式的请求值
     */
    public function actionChangeCategoryAjax()
    {
        $data = \Yii::$app->request->get();
        $list = [];

        // 品类不变，改变类目
        if ($data['type'] == 1) {
            $twoCategory = ShopCategoryProductService::getTwoCategory($data['shop_id'],$data['fid']);
            foreach ($twoCategory as $item) {
                $list[$item['cid']]['name'] = $item['c_name'];
                $list[$item['cid']]['img'] = $item['thumb'];
            }
        }

        // 品牌不变，改变类目
        if ($data['type'] == 2) {
            $brands = ShopCategoryProductService::getBrand($data['shop_id'],$data['fid']);

            // 获取名称的首字母
            function getFirstCharter($str){
                if(empty($str)){return '';}
                $fchar=ord($str{0});
                if($fchar>=ord('A')&&$fchar<=ord('z')) return strtoupper($str{0});
                $s1=iconv('UTF-8','gb2312',$str);
                $s2=iconv('gb2312','UTF-8',$s1);
                $s=$s2==$str?$s1:$str;
                $asc=ord($s{0})*256+ord($s{1})-65536;
                if($asc>=-20319&&$asc<=-20284) return 'A';
                if($asc>=-20283&&$asc<=-19776) return 'B';
                if($asc>=-19775&&$asc<=-19219) return 'C';
                if($asc>=-19218&&$asc<=-18711) return 'D';
                if($asc>=-18710&&$asc<=-18527) return 'E';
                if($asc>=-18526&&$asc<=-18240) return 'F';
                if($asc>=-18239&&$asc<=-17923) return 'G';
                if($asc>=-17922&&$asc<=-17418) return 'H';
                if($asc>=-17417&&$asc<=-16475) return 'J';
                if($asc>=-16474&&$asc<=-16213) return 'K';
                if($asc>=-16212&&$asc<=-15641) return 'L';
                if($asc>=-15640&&$asc<=-15166) return 'M';
                if($asc>=-15165&&$asc<=-14923) return 'N';
                if($asc>=-14922&&$asc<=-14915) return 'O';
                if($asc>=-14914&&$asc<=-14631) return 'P';
                if($asc>=-14630&&$asc<=-14150) return 'Q';
                if($asc>=-14149&&$asc<=-14091) return 'R';
                if($asc>=-14090&&$asc<=-13319) return 'S';
                if($asc>=-13318&&$asc<=-12839) return 'T';
                if($asc>=-12838&&$asc<=-12557) return 'W';
                if($asc>=-12556&&$asc<=-11848) return 'X';
                if($asc>=-11847&&$asc<=-11056) return 'Y';
                if($asc>=-11055&&$asc<=-10247) return 'Z';
                return null;
            }

            // 根据首字母划分
            foreach($brands as $k=>$v) {
                switch(getFirstCharter($v['b_name'])) {
                    case 'A' :
                        $list['A'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'B' :
                        $list['B'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'C' :
                        $list['C'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'D' :
                        $list['D'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'E' :
                        $list['E'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'F' :
                        $list['F'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'G' :
                        $list['G'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'H' :
                        $list['H'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'I' :
                        $list['I'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'J' :
                        $list['J'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'K' :
                        $list['K'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'L' :
                        $list['L'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'M' :
                        $list['M'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'N' :
                        $list['N'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'O' :
                        $list['O'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'P' :
                        $list['P'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'Q' :
                        $list['Q'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'R' :
                        $list['R'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'S' :
                        $list['S'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'T' :
                        $list['T'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'U' :
                        $list['U'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'V' :
                        $list['V'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'W' :
                        $list['W'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'X' :
                        $list['X'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'Y' :
                        $list['Y'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                    case 'Z' :
                        $list['Z'][$v['brand_vid']]['name'] = $v['b_name'];
                        break;
                }
            }
        }

        die(trim($_GET['funcName']).'('.json_encode(array('type'=> $data['type'], 'list' => $list)).')');

    }

}